import Axios from "axios";
const state = {
    activeName: ['second'],
    tableData: [],      //存储账套列表数据
    industry: [],       //存储行业大类
    status: [],       //操作状态   1 == 查看账套信息  2 == 查看系统信息  3 == 系统参数的新增与修改   4 === 新增账套  5 == 查看修改账套信息  6 == 账套修改
    accountSetId: null,       //当前操作的账套id
    yearArr: [],        //会计区间年份
}

const mutations = {
	updateStatus(state, n) {        //更新操作状态
        state.status.splice(0, 1, n);
    },
    updateTableData(state, arr) {
        let tableData = state.tableData;
        tableData.length = 0;
        arr.forEach(item => tableData.push(item));
    },
    updateAccountSetId(state, id) {     //更新账套id
        state.accountSetId = id;
    },
    updateActiveName(state, name) {
        state.activeName.splice(0, 1, name);
    },
    updateYear(state, obj) {			//更新会计区间年度
    	let yearArr = state.yearArr;
    	yearArr.length = 0;
    	for(let i = obj['startYear']; i <= obj['endYear']; i++) {
    		yearArr.push(i);
    	}
    }
}

const actions = {
	getAccountList({state, commit}, $url) {          //获取账套列表数据
        Axios.get($url).then(res => {
			if(res.data.code == 20001) {
				if(res.data.data) {
                    commit('updateTableData', res.data.data);
				}
			}
		});
    },
    getIndustry({state}, $url) {         //获取行业
        Axios.get($url).then(res => {
            if(res.data.code == 20001) {
                if(res.data.data && res.data.data.length > 0) {
                    let data = res.data.data,
                        industry = state.industry;
                    industry.length = 0;
                    data.forEach(item => industry.push(item));
                }
            }
        });
    },
    getYearData({state, commit}, $url) {				//获取会计区间年度
		Axios.get($url).then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    let startYear = res.data.data[0],
                        endYear = res.data.data[res.data.data.length - 2];
                    commit('updateYear', {startYear: startYear, endYear: endYear});
                } else {
                    commit('updateYear', {startYear: new Date().getFullYear() - 20, endYear: new Date().getFullYear()});
                }
            } else {
                commit('updateYear', {startYear: new Date().getFullYear() - 20, endYear: new Date().getFullYear()});
            }
        });
	}
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}